var app = getApp()
var url = app.url
Page({
  data: {
    items: [],//列表
    choice: [],// 已选择
    setupflag: true,
    types: [],
    branch: [],
    AREANAME:"",
    idx:''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var idx = options.idx
    var ob = wx.getStorageSync('area' + idx)
    wx.setNavigationBarTitle({
      title: ob.name,
    })
    var array = ob.array
    this.setData({ items: array, idx: idx })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  showSetup: function () {
    this.setData({ setupflag: false })
  },
  hideSetup: function () {
    this.setData({ types: [] })
    this.setData({ setupflag: true })
  },
  bindBatchChoose: function (e) {
    var items = e.detail.value
    var temp = []
    var types = this.data.types
    if (items.length > 0) {
      if (items.indexOf("1") >= 0) {
        temp.push("1")
        for (var i = 0; i < types.length; i++) {
          if (i > 0) types[i].dis = true
        }
      } else {
        temp = items
        var types = this.data.types
        for (var i = 0; i < types.length; i++) {
          if (i > 0) types[i].dis = false
          else types[i].dis = true
        }
      }
    } else {
      for (var i = 0; i < types.length; i++) {
        types[i].dis = false
      }
    }
    console.log(temp)
    this.setData({ branch: temp, types: types})
  },
  bindChoice: function (e) {
    var that = this
    var item = e.currentTarget.dataset.ob
    if (item.IS_ONLINE === 0) {
      return;
    }
    // var list1 = that.data.choice
    var list2 = [item]
    // var num = 0
    // if (list1.length === 0) {
    //   list2.push(item)
    // } else {
    //   for (var i = 0; i < list1.length; i++) {
    //     if (list1[i].TERMINAL_ADDRESS != item.TERMINAL_ADDRESS) {
    //       list2.push(list1[i])
    //       num++
    //     }
    //   }
    //   if (num === list1.length) {// 当前数据不存在
    //     list2.push(item)
    //   }
    // }

    var types = [{ name: '一支路', value: 2 }, { name: '二支路', value: 3 }
      , { name: '三支路', value: 4 }, { name: '四支路', value: 5 }]
    var result = [{ name: '总路', value: 1 }]
    var switchs = item.SWITCH_STATUS
    for (var i = 0; i < switchs.length; i++) {
      if (switchs[i] != "--") {
        result.push(types[i])
      }
    }
    that.setData({ choice: list2, setupflag: false, types: result, AREANAME: item.AREANAME })
  },
  bingToKeep: function (e) {
    var that = this
    var opy = e.currentTarget.dataset.opy
    var cho = that.data.choice, nos = ''
    if (cho.length == 0){
      wx.showModal({ title: '提示', content: "请选择操作终端号", showCancel: false })
      return
    }else{
      for(var i = 0; i < cho.length; i++){
        if (cho[i].IS_ONLINE == 0){
          wx.showModal({ title: '提示', content: "离线状态不能操作", showCancel: false })
          return
        }
        if (nos == '') {
          nos += cho[i].TERMINAL_ADDRESS
        } else {
          nos += ',' + cho[i].TERMINAL_ADDRESS
        }
      }
    }
    if (this.data.branch.length <= 0) {
      wx.showModal({ title: '提示', content: "请选择支路", showCancel: false })
      return
    }

    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var params = { 
      userName: wx.getStorageSync('userName'), meterAddr: nos, operationId: opy, branch: this.data.branch.join(",")
    }
    var encodingStr = app.ecbEncrypto(params)
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: url + '/xywl/pullSwitchMeter.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.res == 0) {
          var respone = rest.respone
          if (respone.strBackFlag == 1) {
            wx.showModal({
              title: '提示',
              content: '设置成功！',
              showCancel: false,
              success: function () {
                // wx.navigateBack()
                var header = { 'content-type': 'application/x-www-form-urlencoded' }
                var params = { userName: wx.getStorageSync('userName') }
                var encodingStr = app.ecbEncrypto(params)
                wx.request({
                  url: url + '/xywl/appUserMeters.htm',
                  data: encodingStr,
                  method: 'POST',
                  header: header,
                  success: function (res) {
                    that.setData({ types: [] })
                    var decodingStr = app.ecbDecrypto(res.data)
                    var rest = JSON.parse(decodingStr)
                    if (rest.res == 0) {
                      var ll=[];
                      for (var l = 0; l < rest.gasMeterList.length;l++){
                        var st = rest.gasMeterList[l][0].SWITCH_STATUS
                        var result = ['--', '--', '--', '--'];
                        if (st != '' && st != undefined) {
                          // var str1 = st.replace(new RegExp('1', 'gm'), '连接')
                          // var str2 = str1.replace(new RegExp('0', 'gm'), '断开')
                          var sp = st.split('|');
                          for (var j = 0; j < sp.length; j++) {
                            result[j] = sp[j];
                          }
                        }
                        rest.gasMeterList[l][0].SWITCH_STATUS = result;

                        if (rest.gasMeterList[l][0].DEPTNAME === that.data.choice[0].DEPTNAME){
                          ll.push(rest.gasMeterList[l][0])
                        }
                      }
                      that.setData({ items: ll, setupflag:true})
                    } else {
                      // wx.showModal({
                      //   title: '提示',
                      //   content: rest.errorMsg,
                      //   showCancel: false
                      // })
                    }
                  },
                  complete: function () {
                    // wx.stopPullDownRefresh()
                    // that.setData({ showLoading: false })
                  }
                })


              }
            })
          } else {
            that.setData({ types: [] })
            wx.showModal({
              title: '提示',
              content: '设置失败，请重试！',
              showCancel: false
            })
          }

        } else {
          that.setData({ types: [] })
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },

});